//
//  SliderUI.swift
//  uiKit-demo
//
//  Created by 陈澎 on 2024/12/25.
//

import UIKit
class MySliderUI: UIViewController {
    @objc func changeValue(slider: UISlider) {
        print(slider.value)
        if slider.value > 6 {
            let happyImg = UIImage(named: "happyImg")
            slider.setThumbImage(happyImg, for: .normal)
        } else if slider.value < 3{
            let cryImg = UIImage(named: "cryImg")
            let imgSize = CGSize(width: 30, height: 30)
            let renderer = UIGraphicsImageRenderer(size: imgSize)
            let resizedImg = renderer.image { _ in
                cryImg?.draw(in: CGRect(origin: .zero, size: imgSize))
            }
            slider.setThumbImage(resizedImg, for: .normal)
        } else {
            slider.thumbTintColor = UIColor.blue
        }
    }
    func render(_ self: AnyObject, target: Any) {
        let mySlider = UISlider()
        mySlider.frame = CGRect(x: 20, y: 1060, width: 280, height: 30)
        // 滑块控件最大值
        mySlider.maximumValue = 10
        // 滑块控件最小值
        mySlider.minimumValue = 0
        mySlider.value = 5
        // 设置滑块左侧颜色
        mySlider.minimumTrackTintColor = UIColor.red
        // 设置右侧进度条颜色
        mySlider.maximumTrackTintColor = UIColor.green
        mySlider.thumbTintColor = UIColor.blue
        // true：实时触发值的变化 false：只有停止是才会触发change事件
        mySlider.isContinuous = false
        mySlider.addTarget(target, action: #selector(changeValue), for: UIControl.Event.valueChanged)
        self.addSubview(mySlider)
    }
}
